package com.brand.data.model.event;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.List;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.embedded.EmbeddedWebApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.brand.data.model.TipMess;
import com.brand.data.util.TipsUtils;

import lombok.extern.slf4j.Slf4j;

/**
 * 数据表准备监听器
 * @author scl
 *
 */
@Component
@EnableAsync
@Slf4j
public class TipListener implements ApplicationListener<TipEvent>{
	@Autowired
	private EmbeddedWebApplicationContext application;
	@Value(value="${mess.openids}")
	private String openIdStr;
	private String[] openIds;
	@Override
	@Async
	public void onApplicationEvent(TipEvent event) {
		TipMess mess = (TipMess) event.getSource();
//		String url = path+mess.getBrand()+"/tips?detail="+mess.getContent();
		TipsUtils.message(mess.getProject(), mess.getTitle(), mess.getDescrption());
		for (String openId : openIds) {
			TipsUtils.message(openId,mess.getProject(), mess.getTitle(), mess.getDescrption());
		}
		log.info("发送消息提醒成功");
	}
	@PostConstruct
	private void init() {
		if (StringUtils.hasText(openIdStr)) {
			openIds = openIdStr.split(",");
		}
	}
}
